package apt.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import apt.bean.ArtistBean;
import apt.bean.NewsBean;
import apt.common.HibernateUtil;

import com.sun.xml.internal.ws.handler.HandlerException;

public class NewsDao {
	private Session session;
	private Transaction transaction;
	private List<NewsBean> listNews;

	public List<NewsBean> getListNews() {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			String sql = "FROM NewsBean";
			Query query = session.createQuery(sql);
			listNews = query.list();
		} catch (HandlerException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return listNews;
	}

	public boolean addNews(NewsBean news) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			transaction = session.beginTransaction();
			session.save(news);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	public boolean updateNews(NewsBean news) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			transaction = session.beginTransaction();
			session.update(news);
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
		} finally {
			session.close();
		}
		return true;
	}
}
